import datetime
import pytz

import coin.exchange.base.kr_rest.native_public_client_base as npubcb


class HuobiSwapNativePublicClient(npubcb.NativePublicClientBase):
  def __init__(self, timeout=3):
    npubcb.NativePublicClientBase.__init__(self, url="https://api.hbdm.com", timeout=timeout)
    self._valid_kline_period = ['1min', '5min', '15min', '30min', '60min', \
      '4hour', '1day', '1mon']

  def check_kline_period(self, kline_period):
    assert kline_period in self._valid_kline_period, \
        'Invalid kline period!'

  def get_history_kline(self, symbol, kline_period,
                        start_time=None, end_time=None, size=None):
    self.check_kline_period(kline_period)
    url = "swap-ex/market/history/kline"
    params = {
        'contract_code': symbol,
        'period': kline_period,
        'size': size,
        'from': int(start_time.timestamp()),
        'to': int(end_time.timestamp()),
    }
    update = self._get_as_json(url, params)
    return update

  def get_usdt_swap_history_kline(self, symbol, kline_period,
                                  start_time=None, end_time=None, size=None):
    self.check_kline_period(kline_period)
    url = "linear-swap-ex/market/history/kline"
    params = {
        'contract_code': symbol,
        'period': kline_period,
        'size': size,
        'from': int(start_time.timestamp()),
        'to': int(end_time.timestamp()),
    }
    update = self._get_as_json(url, params)
    return update

  def queryAll(self):
    raise NotImplementedError()

if __name__ == "__main__":
  client = HuobiSwapNativePublicClient()
  start_time = datetime.datetime(2020, 11, 15).replace(tzinfo=pytz.UTC)
  end_time = datetime.datetime(2020, 11, 15, 23, 59, 59).replace(tzinfo=pytz.UTC)
  print(client.get_usdt_swap_history_kline(
      'BTC-USDT', '60min', start_time, end_time).msg)
  print(client.get_history_kline('BTC-USD', '60min', start_time, end_time).msg)
